package handle

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yk1/api/basic/config"
	__ "yk1/api/basic/proto"
	"yk1/api/request"
)

func CabinetAdd(c *gin.Context) {
	var req request.CabinetAddReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	r, err := config.Client.CabinetAdd(c, &__.CabinetAddReq{
		Code:        req.Code,
		Address:     req.Address,
		Description: req.Description,
		StationId:   req.StationId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "设备添加失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "设备添加成功",
		"data": r,
	})

}

func CabinetDetails(c *gin.Context) {
	var req request.CabinetDetailsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	r, err := config.Client.CabinetDetails(c, &__.CabinetDetailsReq{
		Id: req.Id,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "设备详情失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "设备详情成功",
		"data": r,
	})

}

func CabinetList(c *gin.Context) {
	r, err := config.Client.CabinetList(c, &__.CabinetListReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "展示失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": r,
	})

}

func ModifyCabinetState(c *gin.Context) {
	var req request.ModifyCabinetStateReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数验证失败",
			"data": err.Error(),
		})
		return
	}
	r, err := config.Client.ModifyCabinetState(c, &__.ModifyCabinetStateReq{
		Id:     req.Id,
		Status: req.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "修改失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": r,
	})

}
